import { SortOrder } from 'antd/es/table/interface';


/**
 * 获取Sorter
 * @param {*} sorter
 */
export const getSorter = (sorter: Record<string, SortOrder>, alias?: string) => {
    let str = '';
    if (Object.keys(sorter).length > 0) {
        const key = Object.keys(sorter)[0];
        const value = sorter[key];

        str = `${key.charAt(0).toUpperCase() + key.slice(1)} ${value === 'descend' ? 'DESC' : 'ASC'}`;
    }

    return str === '' ? {} : { sorter: alias ? `${alias}.${str}` : str };
};

/**
 * 获取Filter
 * @param {*} filter
 */
export const getFilter = (filter: Record<string, (string | number)[] | null>) => {
    const obj: Record<string, any> = {};
    if (!filter) {
        return obj;
    }
    if (Object.keys(filter).length > 0) {
        for (let i = 0; i < Object.keys(filter).length; i++) {
            const key = Object.keys(filter)[i];
            const value = filter[key];
            if (value) {
                obj[key] = value.map((p) => {
                    if (p === 'true') {
                        return true;
                    }
                    if (p === 'false') {
                        return false;
                    }
                    if (new Number(p).toString() === 'NaN') {
                        return p;
                    }
                    if (typeof p === 'number') {
                        return p;
                    }
                    return parseInt(p);
                });
            }
        }
    }
    return obj;
};